//
//  NetManager.m
//  TestAge2016(第六组)
//
//  Created by student on 16/4/11.
//  Copyright © 2016年 wk. All rights reserved.
//

#import "NetManager.h"
#import <AFNetworking.h>
#import "AnalysisHelper.h"
@interface NetManager()
@end

@implementation NetManager

//1. 搜索图片
+(void) searchImagesByName:(NSString *)name
              successBlock:(void(^)(NSArray *imgList))successBlock
                 failBlock:(void(^)(NSError *error))failBlock{
    NSString *urlStr = [NSString stringWithFormat:@"http://www.how-old.net/?q=%@",name];
    urlStr = [urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:urlStr parameters:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (responseObject!=nil) {
            NSArray *arr = [AnalysisHelper analysisImageResult:responseObject];
            successBlock(arr);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failBlock(error);
    }];
}

//2. 测试年龄
+(void)testAgeByImageData:(NSData *)imageData
             successBlock:(void(^)(NSArray *ageInfo))successBlock
                failBlock:(void(^)(NSError *error))failBlock{
    
    NSString *urlStr = @"http://www.how-old.net/Home/Analyze?isTest=False&source=&version=www.how-old.net";
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:urlStr parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        
        [formData appendPartWithFileData:imageData name:@"123" fileName:@"image" mimeType:@"image/jpeg"];
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
//        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
//        NSLog(@"%@",string);
        if (responseObject!=nil) {
            
            NSArray *arr = [AnalysisHelper analysisInfoOfAgeResult:responseObject];
            successBlock(arr);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        failBlock(error);
    }];
    
    
}




@end
